package com.l2j.test.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.l2j.test.common.exception.RemoveApartmentException;
import com.l2j.test.common.result.ResultCodeEnum;
import com.l2j.test.model.entity.*;
import com.l2j.test.model.enums.ItemType;
import com.l2j.test.web.admin.mapper.*;
import com.l2j.test.web.admin.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.l2j.test.web.admin.vo.apartment.ApartmentDetailVo;
import com.l2j.test.web.admin.vo.apartment.ApartmentItemVo;
import com.l2j.test.web.admin.vo.apartment.ApartmentQueryVo;
import com.l2j.test.web.admin.vo.apartment.ApartmentSubmitVo;
import com.l2j.test.web.admin.vo.fee.FeeValueVo;
import com.l2j.test.web.admin.vo.graph.GraphVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {
        boolean isUpdate = apartmentSubmitVo.getId() != null;
        super.saveOrUpdate(apartmentSubmitVo);
        if (isUpdate) {
            LambdaQueryWrapper<GraphInfo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(GraphInfo::getItemId, apartmentSubmitVo.getId());
            queryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            graphInfoService.remove(queryWrapper);

            LambdaQueryWrapper<ApartmentFacility> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(ApartmentFacility::getApartmentId, apartmentSubmitVo.getId());
            apartmentFacilityService.remove(queryWrapper1);

            LambdaQueryWrapper<ApartmentFeeValue> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(ApartmentFeeValue::getApartmentId, apartmentSubmitVo.getId());
            apartmentFeeValueService.remove(queryWrapper2);

            LambdaQueryWrapper<ApartmentLabel> queryWrapper3 = new LambdaQueryWrapper<>();
            queryWrapper3.eq(ApartmentLabel::getApartmentId, apartmentSubmitVo.getId());
            apartmentLabelService.remove(queryWrapper3);
        }
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if(!CollectionUtils.isEmpty(graphVoList)){
            ArrayList<GraphInfo> graphInfos = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfo.setName(graphVo.getName());
                graphInfo.setItemId(apartmentSubmitVo.getId());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfos.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfos);
        }
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        if(!CollectionUtils.isEmpty(facilityInfoIds)){
            ArrayList<ApartmentFacility> facilityInfos = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                ApartmentFacility apartmentFacility = new ApartmentFacility();
                apartmentFacility.setApartmentId(apartmentSubmitVo.getId());
                apartmentFacility.setFacilityId(facilityInfoId);
                facilityInfos.add(apartmentFacility);
            }
            apartmentFacilityService.saveBatch(facilityInfos);
        }
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if(!CollectionUtils.isEmpty(feeValueIds)){
            ArrayList<ApartmentFeeValue> feeValues = new ArrayList<>();
            for (Long feeValueId : feeValueIds) {
                ApartmentFeeValue feeValue = new ApartmentFeeValue();
                feeValue.setApartmentId(apartmentSubmitVo.getId());
                feeValue.setFeeValueId(feeValueId);
                feeValues.add(feeValue);
            }
            apartmentFeeValueService.saveBatch(feeValues);
        }
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        if(!CollectionUtils.isEmpty(labelIds)){
            ArrayList<ApartmentLabel> labels = new ArrayList<>();
            for (Long labelId : labelIds) {
                ApartmentLabel apartmentLabel = new ApartmentLabel();
                apartmentLabel.setApartmentId(apartmentSubmitVo.getId());
                apartmentLabel.setLabelId(labelId);
                labels.add(apartmentLabel);
            }
            apartmentLabelService.saveBatch(labels);
        }

    }

    @Override
    public IPage<ApartmentItemVo> pageItem(Page<ApartmentItemVo> apartmentItemVoPage, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.pageItem(apartmentItemVoPage,queryVo);
    }

    @Override
    public ApartmentDetailVo apartmentDetailInfo(Long id) {
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);
        List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.APARTMENT,id);
        List<LabelInfo> labelInfoList = labelInfoMapper.selectByApartmentID(id);
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectByApartmentId(id);
        List<FeeValueVo> feeValueVoList= feeValueMapper.selectByApartmentId(id);
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        return apartmentDetailVo;
    }

    @Override
    public void removeApartmentById(Long id) {
        LambdaQueryWrapper<RoomInfo> queryWrapper4 = new LambdaQueryWrapper<>();
        queryWrapper4.eq(RoomInfo::getApartmentId, id);
        Long count = roomInfoMapper.selectCount(queryWrapper4);
        if(count > 0){
            throw new RemoveApartmentException(ResultCodeEnum.DELETE_APARTMENT_INCLUDE_ROOM_ERROR);
        }
        super.removeById(id);
        LambdaQueryWrapper<GraphInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GraphInfo::getItemId, id);
        queryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
        graphInfoService.remove(queryWrapper);

        LambdaQueryWrapper<ApartmentFacility> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(ApartmentFacility::getApartmentId, id);
        apartmentFacilityService.remove(queryWrapper1);

        LambdaQueryWrapper<ApartmentFeeValue> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(ApartmentFeeValue::getApartmentId, id);
        apartmentFeeValueService.remove(queryWrapper2);

        LambdaQueryWrapper<ApartmentLabel> queryWrapper3 = new LambdaQueryWrapper<>();
        queryWrapper3.eq(ApartmentLabel::getApartmentId, id);
        apartmentLabelService.remove(queryWrapper3);
    }
}




